//声明js
var time = require('../../utils/util.js')
var formutils = require('../../utils/http.js');
var app = getApp();
Page({
  data: {

    _tabIndex: 2,
    //全局变量
    list: [],
    //加载样式是否显示
    loading: true,
    projectindex: 0,
    project:[],
    xuanzeindex:0,
    xuanze:[],
    industry_id:"",
    project_id: "",
    source_id: "",
    type: '',//默认时间类型:yearyesday,week,month,all
    page: 1,
    searchKey:"",
    isSearch: false,
    listVisable:false,
    isShowTele: true
  },

  onLoad: function(options) {
    this.setData({
      isShowTele: app.globalData.isCustomerTelephone
    })
    if (options.type == 1) {
      this.setData({
        type: "yesterday"
      })
    } else if (options.type == 2) {
      this.setData({
        type: "week"
      })
    } else if (options.type == 3) {
      this.setData({
        type: "month"
      })
    } else {
      this.setData({
        type: ""
      })
    }
    if(options.project_id != undefined && options.project_id != null){
      this.setData({
        project_id: options.project_id
      })
    }

    //获取行业数据
    var that=this;
    wx.request({
      url: app.globalData.httpurl +'industry',
      method: "GET",
      header:{
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success:function(res){
        var adds = [{ "industry_id": "", "name": "行业" }];
        that.setData({
          project: adds.concat(res.data.data)
        })
      }
    })
  //获取项目
    wx.request({
      url: app.globalData.httpurl +'v2/project',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        var adds = [{ "project_id": "", "name": "项目" }];
        that.setData({
          xuanze: adds.concat(res.data.data)
        })
        for(var i = 0; i < that.data.xuanze.length; i++){
          if(that.data.xuanze[i].project_id == options.project_id){
            that.setData({
              xuanzeindex: i
            })
          }
        }
      }
    })

    wx.startPullDownRefresh()

  },
  onPullDownRefresh: function() {
    this.setData({
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  onReachBottom: function() {
    this.setData({
      page: this.data.page+1,
    })
    this.LoadData();
  },
  bindshaixuan: function (e) {
    this.setData({
      projectindex : e.detail.value,
      industry_id: this.data.project[e.detail.value].industry_id,
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  bindshaixuan2: function (e) {
    this.setData({
      xuanzeindex: e.detail.value,
      project_id: this.data.xuanze[e.detail.value].project_id,
      list: [],
      page: 1,
    })
    this.LoadData();
  },
  bindsousuo:function(){
    this.setData({
      isSearch: true,
      listVisable:true,
    })
  },
  LoadData: function() {
    var that = this //很重要，一定要写
    wx.request({
      url: app.globalData.httpurl +'coustomer',
      header:{
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "industry_id":this.data.industry_id,
        "project_id": this.data.project_id,
        "source_id": this.data.source_id,
        "page":this.data.page,
        "search_key": this.data.searchKey,
        "time": this.data.type
      },
      method: "GET",
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        console.log(datas)
        for (var i = 0; i < datas.length; i++) {
          datas[i]["created_at"] = datas[i]["created_at"].substring(0,10)
        }
        that.setData({
          list: that.data.list.concat(datas),
          loading: false
        })
        wx.stopPullDownRefresh();
      },
      fail: function (res) {
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },
  bindPhone:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile,
    })
  },
  bindfenpei:function(){
    wx.navigateTo({
      url: '/pages/persionlist/persionlist',
    })
  },
  bindBtn: function () {
  },
  formhome: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    //权限获取完毕，跳转首页
    if (app.globalData.isHomeXiaoFei ) {
      wx.reLaunch({
        url: '/pages/home3/home3',
      })
    } else {
      wx.reLaunch({
        url: '/pages/home2/home2',
      })
    }
  },
  formkehu: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    if (app.globalData.isCustomerDistribution) {
      wx.navigateTo({
        url: '/pages/fenpeilist/fenpeilist',
      })
    } else {
      wx.navigateTo({
        url: '/pages/list/listCustom',
      })
    }
  },
  formadd: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    wx.navigateTo({
      url: '/pages/menu/menus',
    })
  },
  formPhone:function(e){
    let formid = e.detail.formId
    formutils.postformid(formid)
    console.log(e.currentTarget.dataset.mobile)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile,
    })
  },
  bindkey: function (e) {
    this.setData({
      searchKey: e.detail.value,
      list: [],
      page: 1,
      listVisable: false,
    })
    this.LoadData()
  },
  formcancel: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    this.setData({
      isSearch: false,

    })
  },
})
